/*
 * @Author: zxh
 * @Email: 1271027008@qq.com
 * @Date: 2020-03-26 08:57:21
 * @Descripttion: 
 */
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { VersionService } from './version.service'
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-version',
  templateUrl: './version.component.html',
  styleUrls: ['./version.component.scss']
})
export class VersionComponent implements OnInit {

  constructor(
    private fb: FormBuilder,
    private VersionService:VersionService,
    private message: NzMessageService
  ) { }

  ngOnInit() {
    this.validateForm = this.fb.group({
      version: ['', [Validators.required]],
      isMust:['0']
    });
    this.getVersionList()
  }

  validateForm: FormGroup;
  isVisible:boolean = false
  uploadFileUrl = '/api/common/upload'
  fileUrl:string = ''
  uploadList = []
  addModal(){
    this.isVisible = true
    this.validateForm.reset()
    this.uploadList = []
  }
  handleCancel(){
    this.isVisible = false
  }
  upload($event){
    if($event.type == 'success' || $event.type == "removed"){
      this.uploadList = $event.fileList
    }
  }

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if(this.validateForm.status == 'VALID'){
      this.addVersion(this.validateForm.value)
    }
  }

  addVersion(data){
    if(this.uploadList.length==0){
      this.message.create("error","请上传文件！")
    }else{
      let _data = {
        code:data.version,
        mandatoryUpdate:data.isMust?data.isMust:0,
        url:this.uploadList[0].response[0],
      }
      this.VersionService.addVersion(_data).subscribe(res=>{
        this.isVisible = false
        this.getVersionList()
      })
    }
  }

  pageSize:number = 10
  pageIndex:number = 1
  totalSize:number = 10
  listOfData = []

  clickPage(index){
    this.pageIndex = index
    this.getVersionList()
  }

  getVersionList(){
    let _data = {
      sort:'id,desc',
      page:this.pageIndex - 1,
      size:this.pageSize
    }
    this.VersionService.getList(_data).subscribe(res=>{
      this.listOfData = res.content
      this.totalSize = res.totalElements
    })
  }

}
